package com.kingdee.eas.custom.aoyuan.aytime.basedata.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.framework.app.CoreBillBaseControllerBean;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.IScheduleEntry;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleCollection;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleEntryFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleEntryInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleGroupFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleGroupInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.SchedulesFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.SchedulesInfo;
import com.kingdee.eas.framework.SystemEnum;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.framework.ObjectBaseCollection;
import java.lang.String;
import com.kingdee.eas.framework.CoreBillBaseCollection;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.ScheduleInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;

public class ScheduleControllerBean extends AbstractScheduleControllerBean
{
	private static Logger logger =
		Logger.getLogger("com.kingdee.eas.custom.aoyuan.aytime.basedata.app.ScheduleControllerBean");
	//    @Override
	protected void _quickScheduleSave(Context ctx, ArrayList ar, int year,
			int month, String hrorg) throws BOSException, EASBizException {
		// TODO Auto-generated method stub
		//    	super._quickScheduleSave(ctx, ar, year, month, hrorg);

		try
		{
			HashMap groupmap = new HashMap();
			Iterator it = ar.iterator();
			Calendar c = Calendar.getInstance();
			c.set(Calendar.YEAR, year);
			c.set(Calendar.MONTH, month -1);
			c.set(Calendar.HOUR, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			IScheduleEntry iScheduleEntry = ScheduleEntryFactory.getLocalInstance(ctx);
			while(it.hasNext())
			{
				Map map = (Map)it.next();
				String groupid =(String) map.get("groupid");
				String sDay =(String) map.get("day");
				SchedulesInfo schedules = null;
				if (map.containsKey("val"))
				{
					schedules = (SchedulesInfo)map.get("val");
					if (schedules != null && schedules.getId() != null)
						schedules = SchedulesFactory.getLocalInstance(ctx).getSchedulesInfo(new ObjectUuidPK(schedules.getId().toString()));
					else
						schedules = null;
				}
				String scheduleid = null;
				if (groupmap.containsKey(groupid) ==false)
				{
					scheduleid = this.getScheduleByGroup(ctx, groupid, year, month,hrorg);
					groupmap.put(groupid, scheduleid);
				}
				if (groupmap.containsKey(groupid))
				{
					scheduleid = (String )groupmap.get(groupid);
					c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(sDay));
					FilterInfo filter =new FilterInfo();
					filter.getFilterItems().add(new FilterItemInfo("parent",scheduleid));
					filter.getFilterItems().add(new FilterItemInfo("SchDate",c.getTime()));
					
					IObjectPK [] pks = iScheduleEntry.delete(filter);
					for (int k = 0;k<pks.length;k++)
					{
						iScheduleEntry.delete(pks[k]);
					}
					
					ScheduleEntryInfo entry = new ScheduleEntryInfo();
					ScheduleInfo scheduleInfo = new ScheduleInfo();
					scheduleInfo.setId(BOSUuid.read(scheduleid));
					entry.setParent(scheduleInfo);
					entry.setSchDate(c.getTime());
					entry.setStype(schedules);
					if (schedules != null)
					{
						entry.setIsOffice1(schedules.isIsOffice1());
						entry.setIsOffice2(schedules.isIsOffice2());
						entry.setSt1(schedules.getSt1());
						entry.setSt2(schedules.getSt2());
						entry.setEt1(schedules.getEt1());
						entry.setEt2(schedules.getEt2());
					}
					else
					{
						entry.setIsOffice1(false);
						entry.setIsOffice2(false);
						entry.setSt1(null);
						entry.setSt2(null);
						entry.setEt1(null);
						entry.setEt2(null);
					}
					iScheduleEntry.save(entry);
				}
				else
				{
					continue;
				}
				
				
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	protected String getScheduleByGroup(Context ctx,String groupid,int year, int month ,String hrOrgid)
	{
		try
		{
			EntityViewInfo evi = new EntityViewInfo();
			FilterInfo filter= new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("group",groupid));
			filter.getFilterItems().add(new FilterItemInfo("year",new Integer(year)));
			filter.getFilterItems().add(new FilterItemInfo("month",new Integer(month)));
			evi.setFilter(filter);
			ScheduleCollection col = ScheduleFactory.getLocalInstance(ctx).getScheduleCollection(evi);
			if (col.size() >0)
			{
				return col.get(0).getId().toString();
				
			}
			else
			{
				ScheduleInfo schedule = new ScheduleInfo();
				schedule.setBizDate(new Date());
				HROrgUnitInfo hrOrgUnitInfo= new HROrgUnitInfo();
				hrOrgUnitInfo.setId(BOSUuid.read(hrOrgid));
				schedule.setHROrgUnit(hrOrgUnitInfo);
				ScheduleGroupInfo group = new ScheduleGroupInfo();
				group.setId(BOSUuid.read(groupid));
				schedule.setGroup(group);
				schedule.setYear(year);
				schedule.setMonth(month);
				IObjectPK pk = this.save(ctx, schedule);
				return pk.toString();
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;


	}
}